﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DALBase;
using System.Data;
using System.Collections;
using OneMile.Common;
using OneMile.Entity.Model;

namespace OneMile.DAL
{
   public class DALULogin:BaseDAL
    {
       public DBAccess db = DBConnection.CreateDB();

       public bool IsUserExit(string username)
       {
           try
           {
               StringBuilder sb = new StringBuilder();
               sb.AppendLine("SELECT COUNT(1) FROM Fct_User WHERE Disabled=0 AND UserName=@UserName");
               Hashtable hs = new Hashtable();
               hs.Add("UserName", username);
               int num=Convert.ToInt32(db.ExecuteScalar(sb.ToString(), hs));
               if (num == 1)
               {
                   return true;
               }
               else
               {
                   return false;
               }
           }
           catch (Exception e)
           {
               base.WriteLog(e.Message);
               throw e;
           }
       }

       public EUser UserLogin(string name, string pwd)
       {
           try
           {
               StringBuilder sb = new StringBuilder();
               sb.AppendLine("SELECT ");
               sb.AppendLine("[UserId],");
               sb.AppendLine("[UserName],");
               sb.AppendLine("[Password],");
               sb.AppendLine("[Mobile],");
               sb.AppendLine("[Email],");
               sb.AppendLine("[UserType] ");
               sb.AppendLine("FROM Fct_User WHERE Disabled=0 AND UserName=@UserName AND Password=@Password ");
               Hashtable hs = new Hashtable();
               hs.Add("UserName", name);
               hs.Add("Password", MobileCryptoUtil.MD5(pwd));
               DataTable dt = db.ExecuteQuery(sb.ToString(), hs);
               if (dt.Rows.Count > 0)
               {
                   Guid userid = new Guid(dt.Rows[0]["UserId"].TString());
                   EUser user = new EUser 
                   {
                       UserId = userid,
                       UserName = dt.Rows[0]["UserName"].TString(),
                       Password = dt.Rows[0]["Password"].TString(), 
                       Mobile = dt.Rows[0]["Mobile"].TString(), 
                       Email = dt.Rows[0]["Email"].TString(), 
                       UserType = dt.Rows[0]["UserType"].TInt32()
                   };
                   return user;
               }
               else
               {
                   return null;
               }
           }
           catch (Exception e)
           {
               base.WriteLog(e.Message);
               return null;
           }
       }

       public bool InsertNewUser(EUser user)
       {
           try
           {
               StringBuilder sb = new StringBuilder();
            sb.AppendLine("INSERT INTO Fct_User ");
            sb.AppendLine("(UserId,UserName,Password,Mobile,Email,UserType,Disabled,Rec_CreateTime,Rec_CreateBy,Rec_ModifyTime,Rec_ModifyBy)");
            sb.AppendLine("    VALUES");
            sb.AppendLine("         (");
            sb.AppendLine("           @UserId,");
            sb.AppendLine("           @UserName,");
            sb.AppendLine("           @Password,");
            sb.AppendLine("           @Mobile,");
            sb.AppendLine("           @Email,");
            sb.AppendLine("           @UserType,");
            sb.AppendLine("           @Disabled,");
            sb.AppendLine("           @Rec_CreateTime,");
            sb.AppendLine("           @Rec_CreateBy,");
            sb.AppendLine("           @Rec_ModifyTime,");
            sb.AppendLine("           @Rec_ModifyBy");
            sb.AppendLine("         )");
            Hashtable hs = new Hashtable();
            hs.Add("UserId", user.UserId);
            hs.Add("UserName", user.UserName);
            hs.Add("Password", MobileCryptoUtil.MD5(user.Password));
            hs.Add("Mobile", user.Mobile);
            hs.Add("Email", user.Mobile);
            hs.Add("UserType", user.UserType);
            hs.Add("Disabled", user.Disabled);
            hs.Add("Rec_CreateTime", DateTime.Now);
            hs.Add("Rec_CreateBy", "");
            hs.Add("Rec_ModifyTime",DateTime.Now);
            hs.Add("Rec_ModifyBy", "");
           
            int num = Convert.ToInt32(db.ExecuteNonQuery(sb.ToString(),hs));
            if (num == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
           }
           catch (Exception e)
           {
               base.WriteLog(e.Message);
               throw e;
           }
       }
    }
}
